package com.example.prototype_2;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class ControlFragment extends Fragment {

    private String msg;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View controlLayout = inflater.inflate(R.layout.control_layout,
                container, false);

        /**
         * 控制界面：上下左右按钮
         */
        final Button control_button_down = (Button) controlLayout.findViewById(R.id.button_down);
        final Button control_button_up = (Button) controlLayout.findViewById(R.id.button_up);
        final Button control_button_left = (Button) controlLayout.findViewById(R.id.button_left);
        final Button control_button_right = (Button) controlLayout.findViewById(R.id.button_right);

        /**
         * 设置4个按钮的监听函数
         */
        control_button_down.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                msg = "Down";
                onButtonClickedListener_control.onButtonClicked_control(msg);
            }});

        control_button_up.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                msg = "Up";
                onButtonClickedListener_control.onButtonClicked_control(msg);
            }});

        control_button_left.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                msg = "Left";
                onButtonClickedListener_control.onButtonClicked_control(msg);
            }});

        control_button_right.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                msg = "Right";
                onButtonClickedListener_control.onButtonClicked_control(msg);
            }});

        return controlLayout;
    }

    public interface OnButtonClickedListener_Control {
        public void onButtonClicked_control(String ButtonText);
    }

    private OnButtonClickedListener_Control onButtonClickedListener_control;

    @Override
    public void onAttach(Activity activity){
        super.onAttach(activity);

        try {
            onButtonClickedListener_control = (OnButtonClickedListener_Control) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + "must implement OnButtonClickedListener");
        }
    }



}
